package com.jstyle.jclife.activity;

import android.os.Bundle;
import android.text.Spannable;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.style.AbsoluteSizeSpan;
import android.widget.Button;
import android.widget.ImageView;

import com.jstyle.blesdk.cmdenum.SendCmdState;
import com.jstyle.blesdk.constant.DeviceKey;
import com.jstyle.blesdk.model.ExerciseMode;
import com.jstyle.jclife.R;
import com.jstyle.jclife.ble.BleManager;
import com.jstyle.jclife.utils.DateUtil;
import com.jstyle.jclife.view.TrackStopView;

import java.util.Map;
import java.util.concurrent.TimeUnit;

import butterknife.BindString;
import butterknife.BindView;
import butterknife.ButterKnife;
import io.reactivex.Observable;
import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;

public class DanceActivity extends BaseActivity {

    @BindView(R.id.bt_dance_cal)
    Button btDanceCal;
    @BindView(R.id.bt_dance_time)
    Button btDanceTime;
    @BindView(R.id.bt_dance_hr)
    Button btDanceHr;
    @BindView(R.id.iv_dance_bg)
    ImageView ivDanceBg;
    @BindView(R.id.TrackStopView_dance)
    TrackStopView TrackStopViewDance;
    @BindString(R.string.yoga_cal_laber)
    String yogaCalLabel;
    @BindString(R.string.yoga_hr_laber)
    String yogaHrLabel;
    private Disposable disposableTimer;
    @BindString(R.string.yoga_time_label)
    String yogaTimeLabel;
    @BindString(R.string.yoga_time_label2)
    String yogaTimeLabel2;
    private boolean start;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_dance);
        ButterKnife.bind(this);
        startTimer();
        TrackStopViewDance.setOnStopListener(new TrackStopView.onLongClickListener() {
            @Override
            public void finish() {
                DanceActivity.this.finish();
            }

            @Override
            public void onClick() {
//                if (BleManager.getInstance().isConnected()) {
//                    start = !start;
//                    enableMode(start);
//                   // btYogaStart.setBitmap(start ? R.drawable.yoga_stop : R.drawable.yoga_start);
//                    if (start) {
//                        startTimer();
//                    } else {
//                        unSubscribe(disposableTimer);
//                        btDanceCal.setText("--");
//                        btDanceHr.setText("--");
//                        btDanceTime.setText("--");
//                    }
//
//                }

            }
        });
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        unSubscribe(disposableTimer);
        enableMode(false);
    }

    @Override
    public void dataCallback(Map<String, String> maps, SendCmdState sendCmdState) {
        super.dataCallback(maps, sendCmdState);
        switch (sendCmdState) {
            case ACTIVITY_EXERCISE_DATA:
                String heart = maps.get(DeviceKey.KExerciseHeart);
                String cal = maps.get(DeviceKey.KExerciseCal);
                if (Float.valueOf(heart) == 255) return;

                btDanceCal.setText(getKcalSpannable(cal + yogaCalLabel, yogaCalLabel));
                btDanceHr.setText(getKcalSpannable(heart + yogaHrLabel, yogaHrLabel));
                break;
        }
    }
    private Spannable getKcalSpannable(String text, String label) {

        String startText = label;
        int start = text.indexOf(startText);
        int end = start + startText.length();
        Spannable kcalSpannable = new SpannableString(text);
        setSizeSpannable(kcalSpannable, start, end, 15);
        return kcalSpannable;
    }

    private void setSizeSpannable(Spannable word, int start, int end, int textSize) {
        word.setSpan(new AbsoluteSizeSpan(textSize, true), start, end, Spanned.SPAN_INCLUSIVE_INCLUSIVE);
    }
    private void enableMode(boolean enable) {
        ExerciseMode exerciseMode = new ExerciseMode();
        exerciseMode.setEnableStatus(enable ? ExerciseMode.Status_START : ExerciseMode.Status_FINISH);
        exerciseMode.setExerciseMode(ExerciseMode.Mode_DANCE);
        sendData(SendCmdState.Set_EXERCISE_MODE, exerciseMode);
    }
    public void startTimer() {
        if (disposableTimer != null && !disposableTimer.isDisposed()) return;

        Observable.interval(1, TimeUnit.SECONDS).observeOn(AndroidSchedulers.mainThread()).subscribe(new Observer<Long>() {
            @Override
            public void onSubscribe(Disposable d) {
                disposableTimer = d;
            }

            @Override
            public void onNext(Long aLong) {
                long time = aLong;
                String label=aLong>3600?yogaTimeLabel2:yogaTimeLabel;

                btDanceTime.setText(getKcalSpannable(DateUtil.getSportAxieTime((int) time)+label,label));
                //   btYogaStart.
            }

            @Override
            public void onError(Throwable e) {

            }

            @Override
            public void onComplete() {

            }
        });
    }

}
